/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.util;

import java.io.*;

/**
 *
 * @author Barnet Wagman
 */
abstract public class ParamFileHandler {

    public static final String PARAM_FILE_DIR_PN = "param_file_dir";
    public static final String DIR_FILE_PREFIX = "dir_for_";

    abstract public boolean hasRequiredParams(PropertyList pl);

        /** Reads <tt>filename</tt> in <tt>user.home</tt> if it exists.
         *  If not, it uses the 'param_dir' file in <tt>user.home</tt>
         *  to find the param file (if a 'param_dir' file);
         *
         * @param filename
         * @return parameters from file.
         */
    public PropertyList read(String filename) {

        File dir = getParamDir(filename);
        File paramFile = new File(dir,filename);
        if ( paramFile.exists() ) return(read(paramFile));
        else return(null);
    }

        /** Reads the specified param file.
         *
         * @param pathFile
         * @return params in a <tt>PropertyList</tt> or <tt>null</tt> if
         *  the file does not exists.
         */
    public PropertyList read(File paramFile)  {
        if ( !paramFile.exists() ) return(null);
        if ( paramFile.isDirectory() ) return(null);
        return( PropertyList.parseTextForm(FileUtil.readFile(paramFile)) );
    }

        // ------------------------------------------------

    public void write(File f, PropertyList pl) {
        FileUtil.writeFile(f,pl.toTextForm());
    }

    public void write(String filename, PropertyList pl) {
        File dir = getParamDir(filename);
        write(new File(dir,filename),pl);
    }

    public File getParamDir(String filename) {
        File dirFile = getDirFile(filename);
        if ( (dirFile != null) && dirFile.exists() ) { // use the indirect
            String dirName = (String)
                PropertyList.parseTextForm(
                    FileUtil.readFile(dirFile)).getPropertyValue(PARAM_FILE_DIR_PN);
            return(new File(dirName));
        }
        else { // Use the default
            return(getUserHomeDir());
        }
    }

    private File getDirFile(String filename) {
        String dirFilename = buildDirFilename(filename);
        return(new File(getUserHomeDir(),dirFilename));
    }

    private String buildDirFilename(String filename) {
        return(DIR_FILE_PREFIX + filename);
    }

    public File getUserHomeDir() {
        return(new File(System.getProperty("user.home")));
    }
}
